package com.zdgk.core.web.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zdgk.core.control.config.ControlConfigInfo;
import com.zdgk.core.control.config.MqttConfigInfo;
import com.zdgk.core.web.entity.ConfigInfo;
import com.zdgk.core.web.mapper.ConfigInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ConfigService {
    @Autowired
    private ConfigInfoMapper configInfoMapper;

    public List<ConfigInfo> getAllConfigs() {
        return configInfoMapper.selectList(null);
    }

    public ConfigInfo getByKey(String key) {
        QueryWrapper<ConfigInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("config_key", key);
        return configInfoMapper.selectOne(wrapper);
    }

    public void setKey(ConfigInfo configInfo) {
        List<ConfigInfo> configInfoList = configInfoMapper.selectList(new QueryWrapper<ConfigInfo>().eq("config_key", configInfo.getConfigKey()));
        if (configInfoList.size()>0) {
            ConfigInfo configInfoDb = configInfoList.get(0);
            configInfoDb.setConfigValue(configInfo.getConfigValue());
            configInfoMapper.updateById(configInfoDb);
        }else {
            configInfoMapper.insert(configInfo);
        }
    }

    public void setControlKey(ControlConfigInfo controlConfigInfo) {
        if(StrUtil.isNotEmpty(controlConfigInfo.getMode())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-control-mode");
            configInfo.setConfigValue(controlConfigInfo.getMode());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(controlConfigInfo.getName())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-name");
            configInfo.setConfigValue(controlConfigInfo.getName());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getBaudRate())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-baud-rate");
            configInfo.setConfigValue(controlConfigInfo.getBaudRate().toString());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(controlConfigInfo.getHost())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-network-host");
            configInfo.setConfigValue(controlConfigInfo.getHost());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getPort())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-network-port");
            configInfo.setConfigValue(controlConfigInfo.getPort().toString());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(controlConfigInfo.getServerAddress())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-server-address");
            configInfo.setConfigValue(controlConfigInfo.getServerAddress());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(controlConfigInfo.getServerPointCount())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-server-point-count");
            configInfo.setConfigValue(controlConfigInfo.getServerPointCount());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(controlConfigInfo.getServerSwitchPointCount())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-server-switch-point-count");
            configInfo.setConfigValue(controlConfigInfo.getServerSwitchPointCount());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getSendInterval())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-send-interval");
            configInfo.setConfigValue(controlConfigInfo.getSendInterval().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getReadStartAddress())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-read-start-address");
            configInfo.setConfigValue(controlConfigInfo.getReadStartAddress().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getQuantity())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("read-serial-quantity");
            configInfo.setConfigValue(controlConfigInfo.getQuantity().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getKeepReadStartAddress())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("keep-read-serial-read-start-address");
            configInfo.setConfigValue(controlConfigInfo.getKeepReadStartAddress().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getKeepQuantity())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("keep-read-serial-quantity");
            configInfo.setConfigValue(controlConfigInfo.getKeepQuantity().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getPort104())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("forward-network-port");
            configInfo.setConfigValue(controlConfigInfo.getPort104().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getForwardMode104())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("forward-mode");
            configInfo.setConfigValue(controlConfigInfo.getForwardMode104());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getThreeAdd104())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("forward-three-add");
            configInfo.setConfigValue(controlConfigInfo.getThreeAdd104().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getThreeSwitchAdd104())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("forward-three-switch-add");
            configInfo.setConfigValue(controlConfigInfo.getThreeSwitchAdd104().toString());
            setKey(configInfo);
        }

        if(ObjectUtil.isNotNull(controlConfigInfo.getSlaveMode())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("slaveMode");
            configInfo.setConfigValue(controlConfigInfo.getSlaveMode().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getSlaveName())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("slaveName");
            configInfo.setConfigValue(controlConfigInfo.getSlaveName().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getSlaveBaudRate())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("slaveBaudRate");
            configInfo.setConfigValue(controlConfigInfo.getSlaveBaudRate().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getSlavePort())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("slavePort");
            configInfo.setConfigValue(controlConfigInfo.getSlavePort().toString());
            setKey(configInfo);
        }
        if(ObjectUtil.isNotNull(controlConfigInfo.getSlaveRule())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("slaveRule");
            configInfo.setConfigValue(controlConfigInfo.getSlaveRule().toString());
            setKey(configInfo);
        }
    }

    public ControlConfigInfo getControlKey() {
        ControlConfigInfo controlConfigInfo = new ControlConfigInfo();
        controlConfigInfo.initConfigInfo(configInfoMapper);
        return controlConfigInfo;
    }

    public MqttConfigInfo getMqttKey() {
        MqttConfigInfo mqttConfigInfo = new MqttConfigInfo();
        mqttConfigInfo.initConfigInfo(configInfoMapper);
        return mqttConfigInfo;
    }

    public void setMqttKey(MqttConfigInfo mqttConfigInfo) {
        if(StrUtil.isNotEmpty(mqttConfigInfo.getMqttHost())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("mqtt-host");
            configInfo.setConfigValue(mqttConfigInfo.getMqttHost());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(mqttConfigInfo.getMqttClientId())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("mqtt-client-id");
            configInfo.setConfigValue(mqttConfigInfo.getMqttClientId());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(mqttConfigInfo.getMqttUsername())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("mqtt-username");
            configInfo.setConfigValue(mqttConfigInfo.getMqttUsername());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(mqttConfigInfo.getMqttPassword())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("mqtt-password");
            configInfo.setConfigValue(mqttConfigInfo.getMqttPassword());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(mqttConfigInfo.getMqttUploadTopic())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("mqtt-upload-topic");
            configInfo.setConfigValue(mqttConfigInfo.getMqttUploadTopic());
            setKey(configInfo);
        }
        if(StrUtil.isNotEmpty(mqttConfigInfo.getMqttReceivedTopic())) {
            ConfigInfo configInfo = new ConfigInfo();
            configInfo.setConfigKey("mqtt-received-topic");
            configInfo.setConfigValue(mqttConfigInfo.getMqttReceivedTopic());
            setKey(configInfo);
        }
    }
}
